﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.DXErrorProvider;
using MM.Common.Base;
using MM.Common.DBBase;
using MM.Common.UI.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmGeneralDictionaryTableSetup : XtraForm
    {
        public FrmGeneralDictionaryTableSetup()
        {
            this.centerFlag = "Y";
            InitializeComponent();
        }
        public FrmGeneralDictionaryTableSetup(string groupcode, string code)
        {
            this.centerFlag = "Y";
            this.InitializeComponent();
            this.DT_TB_GROUP.EditValue = groupcode;
            this.DT_TB_MODIFY.EditValue = "0";
            if (code != null)
            {
                this.DT_TB_TABLE_CODE.Text = code;
                this.DT_TB_TABLE_CODE.Properties.ReadOnly = true;
                string commandtext = "SELECT * FROM MXHLY_SYS_DICTIONARY_TABLE WHERE DT_TB_TABLE_CODE = :dictid";
                DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("dictid", code) });
                DataTable dt = new DataTable();
                if (SystemInfo.DbData.Retrieve(commandtext, parm, ref dt) && (dt.Rows.Count == 1))
                {
                    this.DT_TB_TABLE_NAME.FieldValue = dt.Rows[0]["DT_TB_TABLE_NAME"].ToString();
                    this.DT_TB_DISPLAY_NAME.FieldValue = dt.Rows[0]["DT_TB_DISPLAY_NAME"].ToString();
                    this.DT_TB_DESC.FieldValue = dt.Rows[0]["DT_TB_DESC"].ToString();
                    this.DT_TB_GROUP.FieldValue = dt.Rows[0]["DT_TB_GROUP"].ToString();
                    this.DT_TB_MODIFY.FieldValue = dt.Rows[0]["DT_TB_MODIFY"].ToString();
                    this.DT_TB_BIT.FieldValue = dt.Rows[0]["DT_TB_BIT"].ToString();
                }
            }
            else
            {
                this.DT_TB_TABLE_CODE.Properties.ReadOnly = false;
            }
            this.centerFlag = ConfigFunction.GetSystemConfig("CENTERFLAG", "Y", "区域中心使用标志", "C");
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.dxValidationProvider1.Validate())
            {
                DataParamCol parms = new DataParamCol();
                string commandtext = "";
                if (GeneralDictionary.IsDictTableExist(this.DT_TB_TABLE_CODE.Text.Trim()))
                {
                    commandtext = "UPDATE MXHLY_SYS_DICTIONARY_TABLE SET ";
                    string str2 = "";
                    foreach (Control control in base.Controls)
                    {
                        if (control is IControlField)
                        {
                            IControlField field = control as IControlField;
                            string str5 = str2;
                            str2 = str5 + field.FieldDBName + " = :" + field.FieldDBName + ", ";
                            parms.Add(new InputDataParam(field.FieldDBName.Trim(), field.FieldValue));
                        }
                    }
                    commandtext = commandtext + str2.WipeLastSeparator() + " WHERE DT_TB_TABLE_CODE = :table_code";
                    parms.Add(new InputDataParam("table_code", this.DT_TB_TABLE_CODE.Text.Trim()));
                }
                else
                {
                    commandtext = "INSERT INTO MXHLY_SYS_DICTIONARY_TABLE (";
                    string str3 = "";
                    string str4 = "";
                    foreach (Control control2 in base.Controls)
                    {
                        if (control2 is IControlField)
                        {
                            IControlField field2 = control2 as IControlField;
                            str3 = str3 + field2.FieldDBName + ", ";
                            str4 = str4 + ":" + field2.FieldDBName + ", ";
                            parms.Add(new InputDataParam(field2.FieldDBName.Trim(), field2.FieldValue));
                        }
                    }
                    string str6 = commandtext;
                    commandtext = str6 + str3.WipeLastSeparator() + ") VALUES (" + str4.WipeLastSeparator() + ")";
                }
                if (SystemInfo.DbData.ExecuteSql(commandtext, parms) == 1)
                {
                    base.DialogResult = DialogResult.OK;
                    base.Close();
                }
                else
                {
                    XtraMessageBox.Show("加入数据出错！\r\n" + SystemInfo.DbData.ErrorMessage);
                }
            }
        }
        private void FrmGeneralDictionaryTableSetup_Load(object sender, EventArgs e)
        {
            ConditionValidationRule rule = new ConditionValidationRule
            {
                ConditionOperator = ConditionOperator.IsNotBlank,
                ErrorText = "该值不能为空！"
            };
            this.dxValidationProvider1.SetValidationRule(this.DT_TB_TABLE_CODE, rule);
            this.dxValidationProvider1.SetValidationRule(this.DT_TB_TABLE_NAME, rule);
            this.dxValidationProvider1.SetValidationRule(this.DT_TB_DISPLAY_NAME, rule);
            this.dxValidationProvider1.SetValidationRule(this.DT_TB_GROUP, rule);
            this.dxValidationProvider1.SetValidationRule(this.DT_TB_MODIFY, rule);
            if (this.centerFlag == "Y")
            {
                this.DT_TB_GROUP.Enabled = true;
                this.btnOk.Enabled = true;
            }
            else
            {
                this.DT_TB_GROUP.Enabled = false;
                this.btnOk.Enabled = false;
            }
        }

        public string TableCode
        {
            get
            {
                return this.DT_TB_TABLE_CODE.Text;
            }
        }
    }
}
